package com.kyz.speechsdk.utlis;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.List;

/**
 * create by ZhongZihao on 2019/6/10
 */
public class JsonParser {
    public JsonParser() {
    }

    public static String parseIatResult(String json) {
        StringBuffer ret = new StringBuffer();

        try {
            JSONTokener tokener = new JSONTokener(json);
            JSONObject joResult = new JSONObject(tokener);
            JSONArray words = joResult.getJSONArray("ws");

            for (int i = 0; i < words.length(); ++i) {
                JSONArray items = words.getJSONObject(i).getJSONArray("cw");
                JSONObject obj = items.getJSONObject(0);
                ret.append(obj.getString("w"));
            }
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        return ret.toString();
    }

    public static String parseGrammarResult(String json) {
        StringBuffer ret = new StringBuffer();

        try {
            JSONTokener tokener = new JSONTokener(json);
            JSONObject joResult = new JSONObject(tokener);
            JSONArray words = joResult.getJSONArray("ws");

            for (int i = 0; i < words.length(); ++i) {
                JSONArray items = words.getJSONObject(i).getJSONArray("cw");

                for (int j = 0; j < items.length(); ++j) {
                    JSONObject obj = items.getJSONObject(j);
                    if (obj.getString("w").contains("nomatch")) {
                        ret.append("没有匹配结果.");
                        return ret.toString();
                    }

                    ret.append("【结果】" + obj.getString("w"));
                    ret.append("【置信度】" + obj.getInt("sc"));
                    ret.append("\n");
                }
            }
        } catch (Exception var9) {
            var9.printStackTrace();
            ret.append("没有匹配结果.");
        }

        return ret.toString();
    }

    public static String parseLocalGrammarResult(String json) {
        StringBuffer ret = new StringBuffer();

        try {
            JSONTokener tokener = new JSONTokener(json);
            JSONObject joResult = new JSONObject(tokener);
            JSONArray words = joResult.getJSONArray("ws");

            for (int i = 0; i < words.length(); ++i) {
                JSONArray items = words.getJSONObject(i).getJSONArray("cw");

                for (int j = 0; j < items.length(); ++j) {
                    JSONObject obj = items.getJSONObject(j);
                    if (obj.getString("w").contains("nomatch")) {
                        ret.append("没有匹配结果.");
                        return ret.toString();
                    }

                    ret.append("【结果】" + obj.getString("w"));
                    ret.append("\n");
                }
            }

            ret.append("【置信度】" + joResult.optInt("sc"));
        } catch (Exception var9) {
            var9.printStackTrace();
            ret.append("没有匹配结果.");
        }

        return ret.toString();
    }

    public static List<String> parseContactsResult(String json, String key) {
        ArrayList list = new ArrayList();

        try {
            JSONTokener tokener = new JSONTokener(json);
            JSONObject joResult = new JSONObject(tokener);
            JSONArray words = joResult.getJSONArray("userword");

            for (int i = 0; i < words.length(); ++i) {
                JSONObject child = words.getJSONObject(i);
                String name = child.getString("name");
                if (name.equals(key)) {
                    JSONArray childItem = child.getJSONArray("words");

                    for (int j = 0; j < childItem.length(); ++j) {
                        String contact = childItem.get(j).toString();
                        list.add(contact);
                    }
                }
            }
        } catch (Exception var12) {
            var12.printStackTrace();
        }

        return list;
    }
}
